<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片 -->
      <el-card shadow="never">
        <!-- form表单 -->
        <el-form
          label-width="80px"
          :inline="true"
        >
          <el-row>
            <el-col :span="22">
              <el-form-item label="学科名称">
                <el-input v-model="params.subjectName"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  @click="params.subjectName = '' "
                >清除</el-button>
                <el-button
                  type="primary"
                  size="small"
                  @click="search"
                >搜索</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-form-item>
                <el-button
                  type="success"
                  size="small"
                  icon="el-icon-edit"
                  @click.native="openAdd(null,1)"
                >新增学科</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <!-- 消息警告 -->
        <el-row style="margin-bottom:15px;">
          <el-col :span="24">
            <!-- 数据总个数 -->
            <el-alert
              :closable="false"
              show-icon
              :title="`数据一共${counts}条`"
            >
            </el-alert>
          </el-col>
        </el-row>

        <!-- table表格 -->
        <el-row>
          <el-col :span="24">
            <el-table
              :data="list"
              style="width: 100%"
              :header-cell-style="{background:'#eef1f6'}"
              :row-style="{height:'60px'}"
            >
              <el-table-column
                label="序号"
                type="index"
              >
              </el-table-column>
              <el-table-column
                label="学科名称"
                width="150"
                prop="subjectName"
              >
              </el-table-column>
              <el-table-column
                label="创建者"
                prop="username"
              >
              </el-table-column>
              <el-table-column
                label="创建日期"
                prop="addDate"
                width="200"
              >
                <template #default="{row}">
                  {{ formatDateTime(row.addDate) }}
                </template>
              </el-table-column>
              <el-table-column label="前台是否显示">
                <template #default="{row}">
                  {{ isFrontDisplay(row.isFrontDisplay) }}
                </template>
              </el-table-column>
              <el-table-column
                label="二级目录"
                prop="twoLevelDirectory"
              >
              </el-table-column>
              <el-table-column
                label="标签"
                prop="tags"
              >
              </el-table-column>
              <el-table-column
                label="题目数量"
                prop="totals"
              >
              </el-table-column>
              <el-table-column
                label="操作"
                width="230"
              >
                <template #default="{row}">
                  <router-link
                    to=""
                    style="color:#409eff;"
                    @click.native="classify(row)"
                  >学科分类</router-link>
                  <router-link
                    to=""
                    style="padding-left:10px;color:#409eff;"
                    @click.native="autolabel(row)"
                  >学科标签</router-link>
                  <router-link
                    to=""
                    style="padding-left:10px;color:#409eff;"
                    @click.native="openAdd(row,2)"
                  >修改</router-link>
                  <router-link
                    to=""
                    style="padding-left:10px;color:#409eff;"
                    @click.native="remove(row)"
                  >删除</router-link>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>

        <!-- 分页 -->
        <el-row>
          <el-col
            :span="24"
            style="text-align:right; margin-top:20px;"
          >
            <el-pagination
              @size-change="sizeChange"
              @current-change="currentChange"
              :current-page="params.page"
              :page-sizes="[5, 10, 20, 50]"
              :page-size="params.pagesize"
              layout=" prev, pager, next, sizes, jumper"
              :total="counts"
              background
            >
            </el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <AddSubjects
      ref="ad"
      :dialogVisible.sync="dialogVisible"
    />
  </div>
</template>

<script>
// 导入API接口
import { list, remove } from '@/api/hmmm/subjects.js'
// 导入dayjs差插件
import dayjs from 'dayjs'
// 导入新增部门弹层
import AddSubjects from '@/module-hmmm/components/subjects-add.vue'
export default {
  components: {
    AddSubjects
  },
  data () {
    return {
      dialogVisible: false,
      // 数据
      list: [],
      params: {
        // 当前页
        page: 1,
        // 页大小
        pagesize: 10,
        // 学科名称
        subjectName: ''
      },
      // 数据总条数
      counts: 0
    }
  },
  created () {
    this.getList()
  },
  methods: {
    autolabel (row) {
      this.$router.push({
        path: './tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    classify (row) {
      this.$router.push({
        path: './directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 删除按钮
    async remove (id) {
      try {
        await this.$confirm(`此操作将永久删除${id.subjectName}文件`, '提示', { type: 'warning' })
        await remove(id)
        this.getList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 新增修改学科弹窗
    openAdd (row, type) {
      this.dialogVisible = true
      if (type === 2) {
        this.$refs.ad.edUpDate(row)
      }
    },
    // 获取数据
    async getList () {
      const { data } = await list(this.params)
      this.counts = data.counts
      this.list = data.items
    },
    // 页码切换
    currentChange (page) {
      this.params.page = page
      this.getList()
    },
    // 切换每页多少条数据
    sizeChange (size) {
      this.params.pagesize = size
      this.params.page = 1
      this.getList()
    },
    // 格式化前台是否显示
    isFrontDisplay (num) {
      const display = {
        1: '是',
        0: '否'
      }
      return display[num]
    },
    // 格式化时间
    formatDateTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 搜索事件
    async search () {
      const { data } = await list(this.params)
      this.list = data.items
      this.counts = data.counts
    }
  }
}
</script>

<style scoped>
</style>
